#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

static int g_current = -1;

static void add_func(int n)
{
    int i = 0;
    g_current = 0;

    for (i = 1; i <=n; i++) {
        g_current += i;
        sleep(1);
    }
}

static void signal_handler(int sig, siginfo_t* info, void* ucontext)
{
    write(STDOUT_FILENO, "handler begin\n", 14);
    add_func(5);
    write(STDOUT_FILENO, "handler end\n", 12);
}

int main(int argc, char* argv[])
{
    struct sigaction act = {0};
    int obj_sig = atoi(argv[1]);
    int i = 0;
    
    printf("current pid(%d) ...\n", getpid());

    act.sa_sigaction = signal_handler;
    act.sa_flags = SA_RESTART | SA_NODEFER;

    sigaction(obj_sig, &act, NULL);

    add_func(10);

    printf("g_current = %d\n", g_current);

    return 0;
}

